<?php
namespace app\backstage\controller;
use think\Controller;
use think\Db;
use think\facade\Request;

class AdvertStatistics extends Controller
{
    protected $middleware = [
        'VerifyJwt' => ['except' => []],
        'VerifyPower' => ['except' => []],
    ];

    //回收报表
    public function recycling_report()
    {
        $agentId = request()->_role->alias == 'agent' ? request()->_user->id : 0;
        $Account = \think\facade\App::model('Account', 'logic');
        $channels = $Account->getChannels($agentId);

        $this->assign('dimensions',getDimensionList());
        $this->assign('advert_medias',advertMediaList());
        $this->assign('channels',$channels);
        $this->assign('role_name',request()->_role->alias);
        return $this->fetch();
    }

    //回收报表数据获取
    public function recycling_report_data()
    {
        $params = Request::param('', '', 'strip_tags');
        $RecyclingReport = \think\facade\App::model('RecyclingReport', 'logic');

        $json = $RecyclingReport->getList($params);

        $params = Request::param('','','strip_tags,trim');
        if(isset($params['is_total']) && $params['is_total'] == 1)
        {
            $json = $RecyclingReport->getTotal($params);
        }
        return json($json);
    }

    //消耗趋势
    public function cost_report()
    {
        $agentId = request()->_role->alias == 'agent' ? request()->_user->id : 0;
        $Account = \think\facade\App::model('Account', 'logic');
        $channels = $Account->getChannels($agentId);

        $advert_users = [];
        if($channels)
        {
            $where[] = ['channel_id','in',array_column($channels,'id')];
            $AdvertUser = \think\facade\App::model('AdvertUser', 'logic');
            $advert_users = $AdvertUser->getAllList($where);
        }


        $CostReport = \think\facade\App::model('CostReport', 'logic');
        $resultCost = $CostReport->getListScreen(['type'=>'today']);

        $this->assign('cost_trend_x',json_encode($resultCost['data']['cost_trend_x']));
        $this->assign('cost_trend_y',json_encode($resultCost['data']['cost_trend_y']));
        $this->assign('advert_users',$advert_users);
        $this->assign('advert_medias',advertMediaList());
        $this->assign('channels',$channels);
        $this->assign('role_name',request()->_role->alias);
        return $this->fetch();
    }

    //消耗趋势报表
    public function cost_report_data()
    {
        $params = Request::param('', '', 'strip_tags');
        $CostReport = \think\facade\App::model('CostReport', 'logic');

        if(isset($params['type']) && strlen($params['type']) > 1)
        {
            $result = $CostReport->getListScreen($params);

            return sucJson($result['data'], 0);
        }else{
            $result = $CostReport->getList($params);
            return sucJson($result['list'], $result['count']);
        }

    }

    //复充汇总
    public function recharge_repeated()
    {
        $agentId = request()->_role->alias == 'agent' ? request()->_user->id : 0;
        $Account = \think\facade\App::model('Account', 'logic');
        $channels = $Account->getChannels($agentId);

        $this->assign('channels',$channels);
        $this->assign('role_name',request()->_role->alias);
        return $this->fetch();
    }

    //复充汇总数据获取
    public function recharge_repeated_data()
    {
        $params = Request::param('', '', 'strip_tags');
        $RechargeRepeated = \think\facade\App::model('RechargeRepeated', 'logic');
        $result = $RechargeRepeated->getList($params);

        return json(['code'=>1,'msg'=>'ok','data'=>$result['list'],'count'=>$result['count'],'summary'=>$result['summary']]);
    }

    //书籍汇总
    public function book_summary()
    {
        $agentId = request()->_role->alias == 'agent' ? request()->_user->id : 0;
        $Account = \think\facade\App::model('Account', 'logic');
        $channels = $Account->getChannels($agentId);

        $this->assign('channels',$channels);
        $this->assign('role_name',request()->_role->alias);
        return $this->fetch();
    }

    //书籍汇总数据获取
    public function book_summary_data()
    {
        $params = Request::param('', '', 'strip_tags');
        $BookSummary = \think\facade\App::model('BookSummary', 'logic');
        $result = $BookSummary->getList($params);

        return json(['code'=>1,'msg'=>'ok','data'=>$result['list'],'count'=>$result['count'],'summary'=>$result['summary']]);
    }

    //订单统计
    public function order_summary()
    {
        $agentId = request()->_role->alias == 'agent' ? request()->_user->id : 0;
        $Account = \think\facade\App::model('Account', 'logic');
        $channels = $Account->getChannels($agentId);

        $this->assign('channels',$channels);
        $this->assign('role_name',request()->_role->alias);
        return $this->fetch();
    }

    //订单统计数据获取
    public function order_summary_data()
    {
        $params = Request::param('', '', 'strip_tags');
        $OrderSummary = \think\facade\App::model('OrderSummary', 'logic');
        $result = $OrderSummary->getList($params);

        return json(['code'=>1,'msg'=>'ok','data'=>$result['list'],'count'=>$result['count'],'summary'=>$result['summary']]);
    }

}